package com.hellogood.yaohuo.client.basic;

import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hellogood.yaohuo.client.common.CommonMessages;
import com.hellogood.yaohuo.shared.entity.Color;
import com.hellogood.yaohuo.shared.services.ColorService;
import com.smartgwt.client.types.TreeModelType;
import com.smartgwt.client.util.BooleanCallback;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.SelectionChangedHandler;
import com.smartgwt.client.widgets.grid.events.SelectionEvent;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;
import com.smartgwt.client.widgets.tree.TreeNode;

public class ColorController {

	private TreeNode[] treeNodes = null;

	private HLayout layout = null;
	private TreeGrid treeGrid = null;
	private Button addBtn = null;
	private Button delBtn = null;

	public ColorController() {
		initData();
		initView();
	}

	public void initData() {
		ColorService.Util.getInstance().getAllColorList(new GetAllAsyncCallback());
	}

	private void initView() {
		treeGrid = new TreeGrid();
		treeGrid.setFields(new TreeGridField("name", "颜色"));
		treeGrid.setWidth("70%");

		treeGrid.addSelectionChangedHandler(new TreeSelectionHandler());

		addBtn = new Button("增加颜色");
		addBtn.setSize("100", "50");
		addBtn.setMargin(10);
		addBtn.addClickHandler(new AddBtnClickHandler());
		
		delBtn = new Button("删除颜色");
		delBtn.setDisabled(true);
		delBtn.setSize("100", "50");
		delBtn.setMargin(10);		
		delBtn.addClickHandler(new DelBtnClickHandler());

		VLayout btnLayout = new VLayout();
		btnLayout.setMembers(addBtn, delBtn);
		btnLayout.setHeight(500);
		btnLayout.setWidth("30%");

		layout = new HLayout();
		layout.addMember(treeGrid);
		layout.addMember(btnLayout);
		layout.setWidth100();
		layout.setHeight100();
	}

	public Canvas getCanvas() {
		return layout;
	}

	private class GetAllAsyncCallback implements AsyncCallback<List<Color>> {

		@Override
		public void onFailure(Throwable caught) {
			SC.say(caught.getMessage());
		}

		@Override
		public void onSuccess(List<Color> result) {
			if (result != null) {
				treeNodes = new TreeNode[result.size()];
				for (int i = 0; i < result.size(); i++) {
					Color c = result.get(i);
					TreeNode r = new TreeNode();
					r.setAttribute("code", c.getColorCode());
					r.setAttribute("name", c.getColorName());
					r.setAttribute("isOpen", true);
					r.setAttribute("parentId", "-1");
					treeNodes[i] = r;
				}

				Tree tree = new Tree();
				tree.setModelType(TreeModelType.PARENT);
				tree.setNameProperty("name");
				tree.setRootValue("-1");
				tree.setIdField("nodeId");
				tree.setParentIdField("parentId");
				tree.setOpenProperty("isOpen");
				tree.setData(treeNodes);

				treeGrid.setData(tree);
			}
		}
	}

	private class TreeSelectionHandler implements SelectionChangedHandler {

		@Override
		public void onSelectionChanged(SelectionEvent event) {
			ListGridRecord r = treeGrid.getSelectedRecord();
			if (r != null) {
				delBtn.setDisabled(false);
			} else {
				delBtn.setDisabled(true);
			}
		}
	}

	private class AddBtnClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			ColorModifyWindow window = new ColorModifyWindow(ColorController.this);
			window.setSize("400", "300");
			window.centerInPage();
			window.setTitle("创建颜色");
			window.show();
		}

	}

	private class DelBtnClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			ListGridRecord r = treeGrid.getSelectedRecord();
			if (r != null) {
				SC.confirm(CommonMessages.ConfirmDelete, new DeleteBooleanCallback(r));
			} else {
				delBtn.setDisabled(true);
			}
		}

	}

	private class DeleteBooleanCallback implements BooleanCallback {
		private ListGridRecord r;

		public DeleteBooleanCallback(ListGridRecord record) {
			this.r = record;
		}

		@Override
		public void execute(Boolean value) {

			if (value) {
				Color c = new Color();
				c.setColorCode(r.getAttribute("code"));
				c.setColorName(r.getAttribute("name"));
				ColorService.Util.getInstance().deleteColor(c, new AsyncCallback<String>() {

					@Override
					public void onFailure(Throwable caught) {
						SC.say(caught.getMessage());

					}

					@Override
					public void onSuccess(String result) {
						if (result.equals("")) {
							SC.say(CommonMessages.DeleteColorSuccess);
							ColorController.this.initData();
						} else {
							SC.say(result);
						}
					}
				});
			}
		}
	}
}
